package module.scene.view
{
	import com.darwin.darwinBitmapEngine.view.SceneView;
	import com.darwin.darwinDll.Dll;
	import com.darwin.darwinIsometric.core.IsoObject;
	import com.darwin.darwinIsometric.core.IsoWorld;
	import com.darwin.darwinIsometric.core.Point3D;
	import com.darwin.darwinIsometric.core.isoConst.IsoTryResult;
	import com.darwin.darwinIsometric.core.isoConst.IsoType;
	import com.darwin.darwinIsometric.utils.IsoUtils;
	
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import main.App;
	
	import module.scene.FogManager;
	import module.scene.event.JusticeIsoObjectEvent;
	import module.scene.event.JusticeIsoWorldEvent;
	import module.scene.view.iso.base.JusticeIsoObject;
	
	public class JusticeIsoWorld extends IsoWorld
	{
		private var _followUpLayer:Sprite;
		private var _startDragIsoObjPoint:Point3D;
		private var _startDragIsoObjects:Array;
		private var _dragIsoObjectFlag:Boolean;
		private var _dragFlag:Boolean;

		//当取消diy时 被拖动isoObject的原始位置
		private var _originalIsoObjectPoints:Array = [];

		/**
		 *	移动前iso列表中各个组件的位置,必须在调用stopDragIsoObject()获取，否则会被清空
		 * @return 
		 * 
		 */
		public function get originalIsoObjectPoints():Array
		{
			return _originalIsoObjectPoints;
		}

		public function JusticeIsoWorld(sceneView:SceneView, xNum:int=30, zNum:int=30, size:int=32)
		{
			super(sceneView, xNum, zNum, size);
			
			width = App.appStage.stageWidth;
			height = App.appStage.stageHeight;
		}
		/**
		 * 开始拖动世界中的一组显示对象 
		 * @param isoObjectsArray 存储拖动的一组物体
		 * @param startPoint 全局平面坐标
		 */		
		public function startDragIsoObjectList(isoObjectsArray:Array,startPoint:Point):void
		{
			if(isoObjectsArray)
			{
				_startDragIsoObjects = isoObjectsArray;
				//				_testPoint = startPoint;
				var newPoint:Point = new Point(startPoint.x - contentX,startPoint.y - contentY);
				_startDragIsoObjPoint = IsoUtils.screenToIso(newPoint);
				for(var i:int=0;i<_startDragIsoObjects.length;i++)
				{
					_originalIsoObjectPoints.push(_startDragIsoObjects[i].postion3D);					
				}
				_dragIsoObjectFlag = true;
			}
		}
		
		public function updateDragIsoObjectList(endPoint:Point):void
		{
			//			trace(_dragIsoObjectFlag,_dragFlag);
			if(_dragIsoObjectFlag && !_dragFlag)
			{
				endPoint = new Point(endPoint.x - contentX,endPoint.y - contentY);
				var tempPoint3D:Point3D = IsoUtils.screenToIso(endPoint);
				var vx:Number = tempPoint3D.x - _startDragIsoObjPoint.x;
				var vz:Number = tempPoint3D.z - _startDragIsoObjPoint.z;
				
				var endX:int = Math.floor(vx/this.size)*this.size;
				var endZ:int = Math.floor(vz/this.size)*this.size;
				for(var i:int=0;i<_startDragIsoObjects.length;i++)
				{
					var endPoint3D:Point3D = new Point3D(_originalIsoObjectPoints[i].x + endX,0,_originalIsoObjectPoints[i].z + endZ);
					moveIsoObject(_startDragIsoObjects[i],endPoint3D);				
				}
			}
		}
		
		/**
		 * 停止拖动，如果isCancel = true 那么就代表没有移动该物件
		 * 物件会回到原始位置 
		 * @param isCancel
		 * 
		 */		
		override public function stopDragIsoObject(isCancel:Boolean = false):void
		{
			_dragIsoObjectFlag = false;
			if(isCancel && _startDragIsoObjects)
			{
				for(var i:int=0;i<_startDragIsoObjects.length;i++)
				{
					moveIsoObject(_startDragIsoObjects[i],_originalIsoObjectPoints[i]);						
				}
			}
			
			super.stopDragIsoObject(isCancel);
			_originalIsoObjectPoints = [];
			_startDragIsoObjects = null;
		}
		
		
		public function upDateFogContext():void
		{
			if(FogManager.instance)
			{
				FogManager.instance.updateContextXY(contentX,contentY);
			}
		}
		
		public function upFollowUpContext():void
		{
			if(_followUpLayer)
			{
				_followUpLayer.x = contentX;
				_followUpLayer.y = contentY;
			}
		}
		
		/**
		 *	移动一组iso的网格到当前位置 
		 * @param isoList
		 * @param gridColor
		 */
		public function moveGridByList(isoList:Array,gridColor:uint = 0):void
		{
			if(isoList == null || isoList.length == 0)return;
			for (var i:int = 0; i < isoList.length; i++) 
			{
				var $iso:IsoObject = isoList[i] as IsoObject;
				if($iso)
				{
					moveGrid($iso,$iso.postion3D,gridColor);
				}
			}
		}
		
		/**
		 *	测试一组iso是否都能在当前位置放下 
		 * @param isolist
		 * 
		 */
		public function tryHandlerByList(isoList:Array):Boolean
		{
			if(isoList == null || isoList.length == 0)return false;
			for (var i:int = 0; i < isoList.length; i++) 
			{
				var $iso:IsoObject = isoList[i] as IsoObject;
				if($iso)
				{
					var $result:IsoTryResult = tryHandler($iso);
					if($result.isSucess == false)
					{
						return false;
					}
				}
			}
			return true;
		}
		
		/**
		 *	显示一组isoObject身上的网格 
		 * @param list
		 * 
		 */
		public function showGridByList(list:Array):void
		{
			if(list)
			{
				for (var i:int = 0; i < list.length; i++) 
				{
					if(list[i] && list[i] is JusticeIsoObject)
					{
						showGrid(list[i]);
					}
				}
			}
		}
		
		/**
		 *	隐藏一组isoObject身上的网格 
		 * @param list
		 * 
		 */
		public function hideGridByList(list:Array):void
		{
			if(list)
			{
				for (var i:int = 0; i < list.length; i++) 
				{
					if(list[i] && list[i] is JusticeIsoObject)
					{
						hideGrid(list[i]);
					}
				}
			}
		}
		
		/**
		 *	添加一组isoObject到世界中 
		 * @param list 要添加的iso列表
		 * @return 添加完成的iso列表
		 * 
		 */
		public function addToWorldByList(list:Array):Array
		{
			if(list)
			{
				var $iso:JusticeIsoObject;
				for (var i:int = 0; i < list.length; i++) 
				{
					$iso = list[i] as JusticeIsoObject; 
					if($iso)
					{
						this.addToWorld($iso);
					}
				}
			}
			return list;
		}

		/**
		 *	从世界中 移除一组isoObject
		 * @param list 要移除的iso列表
		 * @return 移除的iso列表
		 * 
		 */
		public function removeFromWorldByList(list:Array):Array
		{
			if(list)
			{
				var $iso:JusticeIsoObject;
				for (var i:int = 0; i < list.length; i++) 
				{
					$iso = list[i] as JusticeIsoObject; 
					if($iso)
					{
						this.removeFromWorld($iso);
					}
				}
			}
			return list;
		}
		
		override public function addToWorld(isoObject:IsoObject):IsoObject
		{
			var $iso:JusticeIsoObject = isoObject as JusticeIsoObject;
			super.addToWorld(isoObject);
			$iso.updateFog();
			$iso.dispatchEvent(new JusticeIsoObjectEvent(JusticeIsoObjectEvent.ADD_TO_WORLD));
			var $e:JusticeIsoWorldEvent = new JusticeIsoWorldEvent(JusticeIsoWorldEvent.ISO_ADD_TO_WORLD,$iso);
			dispatchEvent($e);
			return $iso;
		}
		
		override public function removeFromWorld(isoObject:IsoObject, isClearUp:Boolean=true):IsoObject
		{
			var $iso:JusticeIsoObject = isoObject as JusticeIsoObject;
			if($iso.parentWorld)super.removeFromWorld(isoObject,isClearUp);
			else if(isClearUp==true)isoObject.dispose();
			$iso.updateFog();
			$iso.dispatchEvent(new JusticeIsoObjectEvent(JusticeIsoObjectEvent.REMOVE_FROM_WORLD));
			var $e:JusticeIsoWorldEvent = new JusticeIsoWorldEvent(JusticeIsoWorldEvent.ISO_REMOVE_FROM_WORLD,$iso);
			dispatchEvent($e);
			return $iso;
		}
		
		override public function updateDrag(endPoint:Point):void
		{
			super.updateDrag(endPoint);
			upDateFogContext();
			upFollowUpContext();
		}
		
		override public function set contentX(value:Number):void
		{
			super.contentX = value;
			upDateFogContext();
			upFollowUpContext();
		}
		
		override public function set contentY(value:Number):void
		{
			super.contentY = value;
			upDateFogContext();
			upFollowUpContext();
		}
		
		public function setFollowUpLayer(layer:Sprite):void
		{
			_followUpLayer = layer;
		}
		
		public function setMapData():void
		{
			
		}
	}
}